package com.zxw.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxw.common.api.ApiResult;
import com.zxw.common.api.PageResult;
import com.zxw.common.constant.ApiConst;
import com.zxw.common.entity.user.UserEntity;
import com.zxw.common.entity.user.UserListQuery;
import com.zxw.common.entity.user.UserQuery;
import com.zxw.common.entity.user.UserVO;
import com.zxw.common.enums.ResultCode;
import com.zxw.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 用户信息表接口
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-09-08
 */
@Api(tags = "用户信息表接口", description = "UserController")
@RestController
@RequestMapping(ApiConst.API_PREFIX + "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("添加")
    @PostMapping("/save")
    public ApiResult<ResultCode> save(@RequestBody UserQuery userQuery) {
        userService.save(userQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("列表条件分页查询")
    @GetMapping("/pageList")
    public ApiResult<PageResult<UserVO>> pageList(UserListQuery userListQuery) {
        IPage<UserVO> iPage = userService.pageList(userListQuery);
        return ApiResult.success(PageResult.restPage(iPage, iPage.getRecords()));
    }

    @ApiOperation("批量添加")
    @PostMapping("/saveBatch")
    public ApiResult<ResultCode> saveBatch(@RequestBody List<UserQuery> list) {
        userService.saveBatch(list);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public ApiResult<ResultCode> update(@RequestBody UserQuery userQuery) {
        userService.updateById(userQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("批量删除")
    @PostMapping("/deletes")
    public ApiResult<ResultCode> deletes(@RequestBody List<String> ids) {
        userService.removeByIds(ids);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @ApiOperation("详情")
    @GetMapping("/detail/{id}")
    public ApiResult<UserVO> detail(@PathVariable("id") String id) {
        UserEntity userEntity = userService.getById(id);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userEntity, userVO);
        return ApiResult.success(userVO);
    }

    @ApiOperation("根据用户名查询")
    @GetMapping("/queryByUsername")
    public ApiResult<UserVO> queryByUsername(@RequestParam("username") String username) {
        UserVO userVO = userService.queryByUsername(username);
        return ApiResult.success(userVO);
    }

}

